package com.abel.quiz.vo;

import com.abel.quiz.po.AuthRolePo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;

import com.abel.quiz.po.UserInfoPo;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

/**
* @author abel.lin
* @date_time 2017-08-04 13:03
* @description
*/
public class UserInfoDomain extends UserInfoPo implements UserDetails{

    private List<AuthRolePo> roleList;

    public static UserInfoDomain genDomain(UserInfoPo model){
        if(model == null){
            model = new UserInfoPo();
        }
        UserInfoDomain domain = JSON.parseObject(JSON.toJSONString(model), UserInfoDomain.class);
        return domain;
    }

    public static List<UserInfoDomain> genDomainList(List<UserInfoPo> modelList){
        if(modelList == null){
            modelList = new ArrayList<UserInfoPo>();
        }
        List<UserInfoDomain> domainList = JSONArray.parseArray(JSONArray.toJSONString(modelList), UserInfoDomain.class);
        return domainList;
    }

    public List<AuthRolePo> getRoleList() {
        return roleList;
    }

    public void setRoleList(List<AuthRolePo> roleList) {
        this.roleList = roleList;
    }

    /**===================security UserDetails==========================**/
    private Collection<GrantedAuthority> authorities = new HashSet<GrantedAuthority>(); //用户权限集合

    public void setAuthorities(Collection<GrantedAuthority> authorities) {
        this.authorities = authorities;
    }
    /**
     * 返回权限集合
     * @return
     */
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    public String getPassword() {
        return this.getLoginPwd();
    }

    public String getUsername() {
        return this.getLoginName();
    }

    public boolean isAccountNonExpired() {
        return true;
    }

    public boolean isAccountNonLocked() {
        return true;
    }

    public boolean isCredentialsNonExpired() {
        return true;
    }

    public boolean isEnabled() {
        return (this.getEnabled() != null && this.getEnabled()==1);
    }
}
